package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.common.response.JsonResult;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.dto.AddUserDTO;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "用户模块")
@RequestMapping(value = "/v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.插入数据
     */
    @ApiOperation("创建用户")
    @PostMapping(value = "insert")
    //@ResponseBody
    public JsonResult addUser(AddUserDTO addUserDTO) {
        log.debug(addUserDTO.toString());
        User user = new User();
        //复制属性
        BeanUtils.copyProperties(addUserDTO, user);
        user.setCreated(new Date());
        userMapper.insertUser(user);
        return JsonResult.ok();
    }

    /**
     * 2.查询所有用户的姓名和密码
     */
    @ApiOperation("查询所有用户的姓名和密码")
    @GetMapping(value = "userList")
    public JsonResult userList() {
        System.out.println(userMapper.userList());
        return JsonResult.ok(userMapper.userList());
    }

    /**
     * 3.删除指定id的信息
     */
    @ApiOperation("删除指定id的信息")
    @GetMapping(value = "delete")
    @ApiImplicitParam(name = "id", value = "用户id",required = true,dataType = "Long")
    public JsonResult deleteUser(Long id){
        if(id <0){
            throw new IllegalArgumentException("id不能小于0");
        }
        log.debug(id+"");
        userMapper.deleteUserById(id);
        return JsonResult.ok();
    }

    /**
     * 4.更新指定用户
     */
    @ApiOperation("更新指定用户")
    @PostMapping(value = "update")
    public JsonResult updateUser(User user) {
        log.debug(user.toString());
        userMapper.updateUser(user);
        return JsonResult.ok();
    }

    /**
     * 5.更具用户id查询指定用户信息
     */
    @ApiOperation("更具用户id查询指定用户信息")
    @GetMapping(value = "{id}")
    public JsonResult getUserById(@PathVariable int id) {
        log.debug(id+"");
           return JsonResult.ok(userMapper.selectUserById(id));
    }
}
